<!DOCTYPE html>
<script src="../../resources/js-test.js"> </script>
<script src="../../resources/run-after-layout-and-paint.js"> </script>
<script>
var button_was_clicked = false;

function test() {
    if (window.testRunner) {
        testRunner.waitUntilDone();
        testRunner.dumpAsText();
        runAfterLayoutAndPaint(
            function() {
                var iframe = document.getElementById('ScrollIFrame');
                var iframeDocument = iframe.contentDocument;
                var textInIFrame = iframeDocument.getElementById('textInFrame');
                if (window.eventSender) {
                    debug("Starting Autoscroll test on iframe");
                    runAfterLayoutAndPaint(
                        function() {
                            var x = iframe.offsetLeft + textInIFrame.offsetLeft + 7;
                            var y = iframe.offsetTop + textInIFrame.offsetTop + 7;
                            eventSender.dragMode = false;
                            eventSender.mouseMoveTo(x, y);
                            eventSender.mouseDown(1);
                            eventSender.mouseMoveTo(x + 220, y + 220);
                            eventSender.mouseUp(1);
                            runAfterLayoutAndPaint(autoscrollTestPart2);
                        });
                }
            });
    }
}

function autoscrollTestPart2() {
    if (!window.eventSender)
        return;
    var testCompleted = document.getElementById("testCompletedButton");
    var h = testCompleted.offsetTop - document.scrollingElement.scrollTop + 10;
    eventSender.dragMode = false;
    eventSender.mouseMoveTo(20, h);
    // This click exits the pan scroll mode.
    eventSender.mouseDown();
    eventSender.mouseUp();
    // This click actually clicks the button.
    eventSender.mouseDown();
    eventSender.mouseUp();
    runAfterLayoutAndPaint(checkButtonClicked);
}

function checkButtonClicked() {
    if (!button_was_clicked) {
        debug("FAILED : the autoscroll has failed !");
        if (window.testRunner)
            testRunner.notifyDone();
    }
}       

function testCompleted() {
    button_was_clicked = true;
    debug("PASSED : the autoscroll has worked !");
    if (window.testRunner)
        testRunner.notifyDone();
}
</script>

<body onload="test()">
    <p>Test for <a href="http://crbug.com/336373">bug 336373</a>.</p>
    <p>To test manually, hold the middle mouse button on the box with scrollbars and scroll by dragging the mouse outside the bounds of the box. If the bug occurs once the cursor leaves the bounds of the box it will remain stuck in the panning state.</p>
    
    <div id="ScrollableIFrameDiv" style="height: 100px; width: 100px">
        <iframe id="ScrollIFrame" src="resources/iframe-with-overflow-scrollable-div.html" style="height: 100px; width: 100px">
        </iframe>
    </div>

    <button id="testCompletedButton" type="button" onclick="testCompleted()"> Click me </button>
</body>
</html>
